from selenium.webdriver.common.by import By
from Rk_2211A.rk_610.Base import Base

class Page(Base):

    # 元素
    # 豆瓣读书
    book = (By.CLASS_NAME, 'lnk-book')
    # 搜索框
    search_input = (By.ID, 'inp-query')
    # 搜索按钮
    search_button = (By.XPATH, '//*[@id="db-nav-book"]/div[1]/div/div[2]/form/fieldset/div[2]/input')
    # 书籍图片
    book_img = (By.XPATH, '//*[@id="root"]/div/div[2]/div[1]/div[1]/div[3]/div/a/img')
    # 在线阅读
    read_book = (By.XPATH, '//*[@id="content"]/div/div[2]/div[3]/div/div[1]/a')

    # 封装操作步骤
    # 点击豆瓣读书
    def read(self):
        self.click(self.book)
        # 切换页面
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 输入书籍名称
    def search_inp(self):
        self.send_keys(self.search_input, '三国演义')

    # 点击搜索按钮
    def search_but(self):
        self.click(self.search_button)

    # 选择书籍
    def book_check(self):
        self.click(self.book_img)

    # 点击在线阅读
    def online_read(self):
        self.click(self.read_book)
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 业务逻辑
    def luoji(self):
        # 打开豆瓣网站
        self.open_url('https://www.douban.com/')
        # 点击豆瓣读书
        self.read()
        # 输入书籍名称
        self.search_inp()
        # 点击搜索按钮
        self.search_but()
        # 选择书籍
        self.book_check()
        # 点击在线阅读
        self.online_read()